package com.android.djlmapp.adapter.msg;

import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.djlmapp.R;
import com.android.djlmapp.activity.msg.InstationMsgListActivity;
import com.android.djlmapp.activity.msg.SystemMsgListActivity;
import com.android.djlmapp.application.MyApplication;
import com.android.djlmapp.base.BaseActivity;
import com.android.djlmapp.tools.TimeUtils;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @Author wangziheng
 * @ProjectName djlm
 * @ClassName MsgListActivity
 * @package com.android.djlmapp.adapter.msg
 * @data 2018/12/21 18:55
 * @Describe
 * @Change
 */
public class MsgListActivity extends BaseActivity {
    @BindView(R.id.ll_top_back)
    LinearLayout llTopBack;
    @BindView(R.id.top_bac)
    ImageView topBac;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.home_iv_readpoint)
    ImageView homeIvReadpoint;
    @BindView(R.id.top_notes)
    ImageView topNotes;
    @BindView(R.id.iv_top_msg)
    ConstraintLayout ivTopMsg;
    @BindView(R.id.iv_top_set)
    ImageView ivTopSet;
    @BindView(R.id.iv_top_kf)
    ImageView ivTopKf;
    @BindView(R.id.tv_top_jump)
    TextView tvTopJump;
    @BindView(R.id.page_title_layout)
    ConstraintLayout pageTitleLayout;
    private Unbinder mUnbinder;
    /**
     * 系统消息第一条数据的时间
     */
    @BindView(R.id.system_msgList_lastMsgTime)
    TextView mSystemMsgRequestFirstMsgTimeTv;
    /**
     * 系统消息第一条数据内容
     */
    @BindView(R.id.system_msgList_lastMsgContent)
    TextView mSystemMsgRequestFirstMsgContentTv;
    /**
     * 展示请求的系统消息内容的父布局
     */
    @BindView(R.id.request_show_system_msg_layout)
    RelativeLayout mSystemMsgRequestFirstMsgLayout;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_msg_list);
        mUnbinder = ButterKnife.bind(this);
        initView();
        initData();
    }

    @Override
    protected void initView() {
        llTopBack.setVisibility(View.VISIBLE);
        ivTopMsg.setVisibility(View.GONE);
        ivTopSet.setVisibility(View.GONE);
        ivTopKf.setVisibility(View.GONE);
        tvName.setText("消息");
    }

    @Override
    protected void initData() {
        queryLastMsg();
    }


    /**
     * 获取最后一条消息
     */
    private void queryLastMsg() {
        MyApplication.getInstance().getHttpTools().getLastMsg(mStringCallBack);
    }

    private StringCallback mStringCallBack = new StringCallback() {
        @Override
        public void onSuccess(Response<String> response) {
            try {
                String resultStr = response.body();
                JSONObject jsonObject = new JSONObject(resultStr);
                String resuleCode = jsonObject.optString("code");
                if ("200".equals(resuleCode)) {
                    String msgTitle = jsonObject.optString("msg_body");
                    String msgTime = jsonObject.optString("msg_time");
                    if (!TextUtils.isEmpty(msgTitle)) {
                        mSystemMsgRequestFirstMsgLayout.setVisibility(View.VISIBLE);
                        mSystemMsgRequestFirstMsgContentTv.setText(msgTitle);
                        if (!TextUtils.isEmpty(msgTime)) {
                            mSystemMsgRequestFirstMsgTimeTv.setText(TimeUtils.timedate(msgTime));
                        }
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        @Override
        public void onError(Response<String> response) {
            super.onError(response);
        }
    };

    @OnClick({R.id.msg_instation_layout, R.id.msg_custom_layout, R.id.msg_system_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.msg_instation_layout:
                startActivity(new Intent(this, InstationMsgListActivity.class));
                break;
            case R.id.msg_custom_layout:
                goToMeiQia();
                break;
            case R.id.msg_system_layout:
                startActivity(new Intent(this, SystemMsgListActivity.class));
                break;
            default:
                break;
        }
    }

    @OnClick({R.id.ll_top_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_top_back:
                finish();
                break;
                default:break;
        }
    }
}
